package com.share.controller.v1;

import com.alibaba.fastjson.JSON;
import com.share.common.enums.AccountTypeEnum;
import com.share.constant.SecurityConstant;
import com.share.properties.GiteeProperties;
import com.share.properties.WeChatProperties;
import com.share.service.OAuthService;
import com.share.service.WebSocketService;
import com.share.user.Gitee.GiteeUserInfo;
import com.share.user.Gitee.ProviderToken;
import com.share.user.admin.dtos.PayloadDTO;
import com.share.user.admin.vo.LoginVo;
import com.share.user.weChat.dtos.TokenInfo;
import com.share.user.weChat.dtos.WxUserInfo;
import com.share.util.common.ControllerUtils;
import com.share.utils.TokenUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/user/v1")
public class OAuthCallbackController {

    private final OAuthService oAuthService;

    @Autowired
    public OAuthCallbackController(OAuthService oAuthService) {
        this.oAuthService = oAuthService;
    }


    /**
     * 返回微信用户信息
     */
    @RequestMapping(value = "/weChatCallback")
    public String wxCallback(@RequestParam("code") String code,
                             @RequestParam("state") String state,
                             HttpServletRequest request, HttpServletResponse response,
                             Model model) throws Exception {


        // 返回index.html页面
        return oAuthService.thirdPartyLogin(AccountTypeEnum.WECHAT,code);
    }




    /**
     * Gitee请求回调
     */
    @GetMapping("/giteeCallback")
    public String callback(@RequestParam("code") String code,
                           @RequestParam("state") String state,
                           Model model) throws IOException {

        return oAuthService.thirdPartyLogin(AccountTypeEnum.GITE,code);
    }


}
